package com.zt.rongda.asset.service;

import java.text.SimpleDateFormat;
import java.util.List;

import com.zt.rongda.asset.inter.AssetMapper;
import com.zt.rongda.asset.inter.AssetTransferMapper;
import com.zt.rongda.asset.model.Asset;
import com.zt.rongda.asset.model.AssetTransfer;
import com.zt.system.model.BusinessException;
import com.zt.servlet.ReqBean;
import com.zt.system.model.SqlBuilder;
import com.zt.system.service.BaseService;
import com.zt.util.DateUtil;
import com.zt.util.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AssetService extends BaseService {

    @Autowired
    private AssetMapper assetMapper;
    @Autowired
    private AssetTransferService assetTransferService;

    /**
    * 查询
    *
    * @param reqBean
    * @return
    */
    public JSONObject getTable(ReqBean reqBean) {
        SqlBuilder sqlBuilder = new SqlBuilder("");

        sqlBuilder.appendSql("select * from ASSET where 1=1 ");
        sqlBuilder.addWhereLike2("ASSET_CODE", reqBean.getInfo("assetCode"));
        sqlBuilder.addWhereLike2("ASSET_NAME", reqBean.getInfo("assetName"));
        sqlBuilder.addWhereLike2("ASSET_CLASS", reqBean.getInfo("assetClass"));
        sqlBuilder.addWhereLike2("C_TYPE", reqBean.getInfo("type"));

        // 时间间隔
        String endDate = reqBean.getInfo("purchaseDateEnd");
        if(endDate != null && endDate.length()>=10)
        {
            Date tmp_date =  DateUtil.parse(endDate);

            SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
            endDate = df.format(new Date(tmp_date.getTime() + 1 * 24 * 60 * 60 * 1000)); // 结束日期加一天
        }
        sqlBuilder.addTimeWhere("PURCHASE_DATE", reqBean.getInfo("purchaseDateStart"), ">=");
        sqlBuilder.addTimeWhere("PURCHASE_DATE", endDate, "<");
        //

        return queryTable(sqlBuilder, reqBean.getPageSize(), reqBean.getPageIndex());
    }

    public String queryType(ReqBean reqBean){
        String cTypeId = reqBean.getInfo("typeId");
        String sql = "select CODE_NAME typeName from SYS_CODE where CODE_TYPE='assettype' and CODE_VALUE = " + cTypeId;
        return this.query(sql).getJSONObject(0).getString("typeName");
    }

    /**
    * 通过id查询
    *
    * @param assetId
    * @return
    */
    public Asset getById(String assetId) {
        return assetMapper.selectByPrimaryKey(assetId);
    }

    /**
    * 新增
    *
    * @param asset
    */
    public void add(Asset asset) {
        asset.setAssetId(stringId());// 手动设置Id

        asset.setMakeTime(new Date());
        asset.setModifyTime(asset.getMakeTime());
        assetMapper.insert(asset);

        AssetTransfer assetTransfer = new AssetTransfer();
        assetTransfer.setAssetId(asset.getAssetId());
        assetTransfer.setUserId(asset.getUserId());
        assetTransfer.setUserName(asset.getUserName());
        assetTransfer.setDeptId(asset.getDeptId());
        assetTransfer.setDeptName(asset.getDeptName());
        assetTransfer.setUseDate(DateUtil.parse(asset.getUseDate(),"yyyy-MM-dd"));
        assetTransfer.setLocation(asset.getLocation());
        assetTransfer.setStatus("1");
        assetTransfer.setOperator(asset.getOperator());

        assetTransferService.add(assetTransfer);
    }

    /**
    * 修改
    *
    * @param asset
    */
    public void update(Asset asset) {
        String assetId = asset.getAssetId();
        if (StringUtil.isEmpty(assetId)) {
            throw new BusinessException("获取主键失败");
        }

        //有异动，新增历史表
        JSONObject joAssetOld = JSONObject.fromObject(asset.getAssetOld());

        if( !asset.getUserId().equals(joAssetOld.getString("userId")) || !asset.getDeptId().equals(joAssetOld.getString("deptId")) || !asset.getLocation().equals(joAssetOld.getString("location")) ){

            this.update("update ASSET_TRANSFER set STATUS = '0' where ASSET_ID = ? ", new Object[]{assetId});

            AssetTransfer assetTransfer = new AssetTransfer();
            assetTransfer.setAssetId(asset.getAssetId());
            assetTransfer.setUserId(asset.getUserId());
            assetTransfer.setUserName(asset.getUserName());
            assetTransfer.setDeptId(asset.getDeptId());
            assetTransfer.setDeptName(asset.getDeptName());
            assetTransfer.setUseDate(DateUtil.parse(asset.getUseDate(),"yyyy-MM-dd"));
            assetTransfer.setLocation(asset.getLocation());
            assetTransfer.setStatus("1");
            assetTransfer.setOperator(asset.getOperator());

            assetTransferService.add(assetTransfer);
        }

        asset.setModifyTime(new Date());
        assetMapper.updateByPrimaryKeySelective(asset);
    }

    /**
    * 删除
    *
    * @param assetId
    */
    public void delete(String assetId) {
        if (StringUtil.isEmpty(assetId)) {
            throw new BusinessException("获取主键失败");
        }
        assetMapper.deleteByPrimaryKey(assetId);
    }

}